package com.zqc.wms.controller;

import com.zqc.wms.entity.APIResponse;
import com.zqc.wms.entity.SessionUser;
import com.zqc.wms.entity.UserInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @PostMapping("/login")
    public APIResponse login(@RequestBody SessionUser sessionUser) {
        UsernamePasswordToken token = new UsernamePasswordToken(sessionUser.getUsername(), sessionUser.getPassword());
        SecurityUtils.getSubject().login(token);
        return getInfo();
    }

    @PostMapping("/logout")
    public APIResponse logout() {
        SecurityUtils.getSubject().logout();
        return APIResponse.success();
    }

    @GetMapping("/info")
    public APIResponse getInfo() {
        return APIResponse.success((UserInfo) SecurityUtils.getSubject().getPrincipal());
    }
}
